package com.tistory.commons.utils;

import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;

import org.apache.log4j.Appender;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

public class OmcIntLogUtil {
	public static Logger loggerUaps = Logger.getLogger(OmcIntLogUtil.class);
	public static Logger loggerOmpdl = Logger.getLogger(OmcIntLogUtil.class);
	public static Logger loggerVsms = Logger.getLogger(OmcIntLogUtil.class);
	public static Logger loggerFeedbox = Logger.getLogger(OmcIntLogUtil.class);
	
	public static DailyRollingFileAppender uapsAppender = null;
	public static DailyRollingFileAppender ompdlAppender = null;
	public static DailyRollingFileAppender vsmsAppender = null;
	public static DailyRollingFileAppender feedboxAppender = null;
	
	public static void writeLog(String task, StringBuffer msg)
	{
		loggerUaps.setLevel(Level.INFO);//debug code 생략
		loggerOmpdl.setLevel(Level.INFO);//debug code 생략
		loggerVsms.setLevel(Level.INFO);//debug code 생략
		loggerFeedbox.setLevel(Level.INFO);//debug code 생략
		
		  try {
		   String logPattern =  "%m%n";
		// 날짜 패턴에 따라 추가될 파일 이름
	        String datePattern = "'.'yyyyMMdd";
	        String dir =getDir(task);
	        
			if("uaps".equals(task)  )
			{
				if(uapsAppender == null)
				{
					uapsAppender = new DailyRollingFileAppender(new PatternLayout(logPattern), dir,datePattern );
					loggerUaps.addAppender(uapsAppender);
				}
				loggerUaps.info(getSysDate("yyyyMMdd HH:mm:ss,SSS")+"^"+msg.toString());		
			}
			else if("ompdl".equals(task) )
			{
				if(ompdlAppender == null)
				{
					ompdlAppender = new DailyRollingFileAppender(new PatternLayout(logPattern), dir,datePattern );
					loggerOmpdl.addAppender(ompdlAppender);
				}
				loggerOmpdl.info(getSysDate("yyyyMMdd HH:mm:ss,SSS")+"^"+msg.toString());		

			}
			else if("vsms".equals(task) )
			{
				if(vsmsAppender == null)
				{
					vsmsAppender = new DailyRollingFileAppender(new PatternLayout(logPattern), dir,datePattern );
					loggerVsms.addAppender(vsmsAppender);
				}
				loggerVsms.info(getSysDate("yyyyMMdd HH:mm:ss,SSS")+"^"+msg.toString());		

			}
			else if("feedbox".equals(task) )
			{
				if(feedboxAppender == null)
				{
					feedboxAppender = new DailyRollingFileAppender(new PatternLayout(logPattern), dir,datePattern );
					loggerFeedbox.addAppender(feedboxAppender);
				}
				loggerFeedbox.info(getSysDate("yyyyMMdd HH:mm:ss,SSS")+"^"+msg.toString());		
			}
			
		  } catch (IOException e) {
			e.printStackTrace();
			System.out.println("로그설정에 문제");
		  }
	}
	
	private static String getDir(String intfName)
	{
		String dir="";
		if("uaps".equals(intfName))
			dir="/log/omc/userpoc/uaps/uaps.log";//"C:\\uaps\\";//
		else if("ompdl".equals(intfName))
			dir="/log/omc/userpoc/ompdl/ompdl.log";
		else if("vsms".equals(intfName))
			dir="/log/omc/userpoc/vsms/vsms.log";
		else if("feedbox".equals(intfName))
			dir="/log/omc/userpoc/feedbox/feedbox.log";
		return dir;
	}
	
	
    public static String getSysDate( String pattern )
    {
        return getFormatString( pattern,  Calendar.getInstance(Locale.KOREA) );
    }
    private static String getFormatString(String pattern, Calendar cal)
    {
        java.text.SimpleDateFormat formatter =  new java.text.SimpleDateFormat(pattern, java.util.Locale.KOREA);
        return  formatter.format(cal.getTime());
    }
    	
}
